package com.demo.core.dao;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.demo.base.model.system.EntityBase;
import com.demo.base.util.StringUtil;

public class BaseDao {

	@Autowired
	private SessionFactory sessionFactory;
	
	public Session getSession(){
		
		return sessionFactory.getCurrentSession();
	}
	
	/**
	 * 保存对象实体
	 * @param object
	 * @return
	 */
	public void add(Object object){
		
		Session session = getSession();
		session.save(object);
	}
	
	/**
	 * 删除实体对象
	 * @param object
	 */
	public void delete(Object object){
		
		Session session = getSession();
		session.delete(object);
	}
	
	/**
	 * 修改实体对象
	 * @param object
	 */
	public void update(Object object){
		Session session = getSession();
		session.update(object);
		session.flush();
	}
	
	/**
	 * 得到实体对象
	 * @param clz
	 * @param id
	 * @return
	 */
	public Object get(Class<?> clz,String id){
		
		Session session = getSession();
		
		return session.get(clz, id);
	}
	
	/**
	 * 拼装Sql条件  
	 * @param value
	 * @param sql
	 * @param cxslit
	 * @param appendSql
	 */
	protected void addSqlWhere(Object value, StringBuffer sql, List<Object> args, String appendSql) {
		this.addSqlWhere(value, sql, args, appendSql, 1);
	}

	/**
	 * 拼装Sql条件  
	 * @param value
	 * @param sql
	 * @param cxslit
	 * @param appendSql
	 * @param repetCount
	 */
	protected void addSqlWhere(Object value, StringBuffer sql, List<Object> args, String appendSql, int repetCount) {
		if (value != null && (value instanceof String ? !StringUtil.isBlank(value.toString()) : true)) {
			sql.append(appendSql);
			for (int i = 0; i < repetCount; i++) {
				args.add(value);
			}
		}
	}

	/**
	 * 拼装Sql条件  like
	 * @param value
	 * @param sql
	 * @param cxslit
	 * @param appendSql
	 */
	protected void addSqlWhereLike(Object value, StringBuffer sql, List<Object> args, String appendSql) {
		this.addSqlWhereLike(value, sql, args, appendSql, 1);
	}

	/**
	 * 拼装Sql条件  like
	 * @param value
	 * @param sql
	 * @param args
	 * @param appendSql
	 * @param repetCount
	 */
	protected void addSqlWhereLike(Object value, StringBuffer sql, List<Object> args, String appendSql, int repetCount) {
		if (value != null && (value instanceof String ? !StringUtil.isBlank(value.toString()) : true)) {
			sql.append(appendSql);
			for (int i = 0; i < repetCount; i++) {
				args.add("%" + value + "%");
			}
		}
	}

	/**
	 * 拼装Sql条件  like
	 * @param value
	 * @param sql
	 * @param cxslit
	 * @param appendSql
	 */
	protected void addSqlWhereLeftLike(Object value, StringBuffer sql, List<Object> args, String appendSql) {
		if (value != null && (value instanceof String ? !StringUtil.isBlank(value.toString()) : true)) {
			sql.append(appendSql);
			args.add("%" + value);
		}
	}

	/**
	 * 拼装Sql条件  like
	 * @param value
	 * @param sql
	 * @param cxslit
	 * @param appendSql
	 */
	protected void addSqlWhereRightLike(Object value, StringBuffer sql, List<Object> args, String appendSql) {
		if (value != null && (value instanceof String ? !StringUtil.isBlank(value.toString()) : true)) {
			sql.append(appendSql);
			args.add(value + "%");
		}
	}

	/**
	 * 拼装Sql条件  in 或者 not in
	 * @param values
	 * @param sql
	 * @param cxslit
	 * @param appendSql
	 */
	protected void addSqlWhereIn(Object[] values, StringBuffer sql, List<Object> args, String appendSql) {
		if (values != null && values.length > 0) {
			StringBuffer str = new StringBuffer();
			str.append("(");
			for (int i = 0; i < values.length; i++) {
				str.append("?");
				if (i != values.length - 1) {
					str.append(",");
				}
				args.add(values[i]);
			}
			str.append(")");
			sql.append(appendSql.replace("?", str.toString()));
		}
	}

	/**
	 * save 一个对象，如果没有id，新生成一个对象，如果有id，更新一个对象
	 * 返回对象类型不是Entity时可用此函数灵活查询.
	 *
	 * @param entity
	 *            数量可变的参数
	 */
	public <T extends EntityBase>  T saveEntity(T entity) throws Exception{
		Session s = getSession();
		try {
			if (StringUtils.isEmpty(entity.getId())) {//新增
				s.save(entity);
			}else {
				s.merge(entity);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		}
		return entity;
	}

	/**
	 * 执行Update或Delete的hql
	 * @param hql
	 * @param values
	 * @return
	 */
	public int hExecuteUpdate(String hql, Object...values) {
		Session s = getSession();
		int retval = 0;
		try {
			Query query = s.createQuery(hql);
			if (values != null) {
				for (int i = 0; i < values.length; i++) {
					query.setParameter(i, values[i]);
				}
			}
			retval = query.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return retval;
	}

	/**
	 * 根据查询HQL与返回对象类型创建Query对象,带参数。
	 * @param hql
	 * @param entityClazz
	 * @param values
	 * @param <T>
	 * @return 返回List对象
	 */
	public <T> List<T> hFind(String hql, Class<T> entityClazz, final Object... values) {
		Session s = getSession();
		List<T> retval = null;
		try {
			Query query = s.createQuery(hql);
			if (values != null) {
				for (int i = 0; i < values.length; i++) {
					query.setParameter(i, values[i]);
				}
			}
			retval = query.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return retval;
	}
}
